package cn.fuego.busrental.ui.StyleTour;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import cn.fuego.busrental.R;
import cn.fuego.busrental.ui.user.ContainBusOrderActivity;
import cn.fuego.busrental.webservice.up.model.ContainBusJson;
import cn.fuego.common.log.FuegoLog;
import cn.fuego.common.util.format.DateUtil;
import cn.fuego.misp.ui.common.edit.MispEditParameter;
import cn.fuego.misp.ui.common.edit.MispTextEditActivity;
import cn.fuego.misp.ui.info.MispInfoListActivity;
import cn.fuego.misp.ui.model.CommonItemMeta;
import cn.fuego.misp.ui.pop.MispPopListWindow;
import cn.fuego.misp.ui.pop.MispPopWindowListener;
import cn.fuego.misp.ui.util.DateTimePickDialogUtil;
import cn.fuego.misp.ui.util.StrUtil;

public class ContainBusActivity extends MispInfoListActivity {

private FuegoLog log = FuegoLog.getLog(getClass());
	
	public static final String StartPlace = "出发地点";
	public static final String EndPlace = "目的地点";
	public static final String StartTime = "发车时间";	
	public static final String ReturnTime = "返回时间";
	public static final String StartBusPlace = "发车地点";
	public static final String BusStyle= "车型";
	public static final String Sum = "人数";		
	public static final String Free_Ticket = "免票儿童数";	
	private static final int REQUEST_CODE_EDIT_TEXT = 0;
	private static final int REQUEST_CODE_EDIT_ADDR = 1;
	//相关组件
	private ContainBusJson containbusjson=new ContainBusJson();
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}
	
	public static void jump(Context context)
	{
		Intent intent = new Intent();
		intent.setClass(context, ContainBusActivity.class);		
		context.startActivity(intent);
	}

	@Override
	public void initRes()
	{
		this.activityRes.setAvtivityView(R.layout.activity_contain_bus);
		this.listViewRes.setListView(R.id.contain_bus_listview);
		this.activityRes.setName(this.getString(R.string.activity_contain_bus));		
 		this.activityRes.getButtonIDList().add(R.id.contain_bus_btn);	
		this.getDataList().clear();
		this.getDataList().addAll(getBtnData(true));	
	}

	@Override
	protected
	void initData()
	{
		
	}


	@Override
	public void onClick(View v)
	{
		switch(v.getId()){
		case R.id.contain_bus_btn:
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this).setMessage("提交成功");
			 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						ContainBusOrderActivity.jump(ContainBusActivity.this);
						finish();
						
					}
				
				});
			
				  builder.create();
				  builder.show();
				 
			
			break;
		}
	}

	protected List<CommonItemMeta> getBtnData(boolean isInit)
	{			
		
		List<CommonItemMeta> list = new ArrayList<CommonItemMeta>();
				
			list.add(new CommonItemMeta(CommonItemMeta.TEXT_CONTENT, StartPlace, StrUtil.noNullStr(containbusjson.getStartplace())));
			list.add(new CommonItemMeta(CommonItemMeta.TEXT_CONTENT, EndPlace, StrUtil.noNullStr(containbusjson.getAimplace())));
			list.add(new CommonItemMeta(CommonItemMeta.TEXT_CONTENT, StartTime, StrUtil.noNullStr(containbusjson.getStarttime())));
			list.add(new CommonItemMeta(CommonItemMeta.TEXT_CONTENT, ReturnTime,  StrUtil.noNullStr(containbusjson.getReturntime())));
			list.add(new CommonItemMeta(CommonItemMeta.TEXT_CONTENT, StartBusPlace,  StrUtil.noNullStr(containbusjson.getStartbusplace())));				
			list.add(new CommonItemMeta(CommonItemMeta.TEXT_CONTENT, BusStyle,  StrUtil.noNullStr(containbusjson.getBusstyle())));
			list.add(new CommonItemMeta(CommonItemMeta.TEXT_CONTENT, Sum, StrUtil.noNullStr(containbusjson.getSum()+"")));	
			list.add(new CommonItemMeta(CommonItemMeta.TEXT_CONTENT, Free_Ticket, StrUtil.noNullStr(containbusjson.getChildrensum()+"")));	
		return list;
	}


 	@Override
	public void onItemListClick(AdapterView<?> parent, View view, long id,
			CommonItemMeta item)
	{
 		String title =   item.getTitle();
		MispEditParameter data = new MispEditParameter();
		data.setTilteName(title);
		data.setDataKey(title);
		if(StartPlace.equals(title))
		{
			data.setDataValue((String) item.getContent());
			InfoEditActivity.jump(this, data, REQUEST_CODE_EDIT_TEXT,this.getString(R.string.disc_order_number));
		}		
		if(EndPlace.equals(title))
		{

			data.setDataValue((String) item.getContent());
			InfoEditActivity.jump(this, data, REQUEST_CODE_EDIT_TEXT,"");
		}

		if(StartTime.equals(title))
		{
			data.setDataValue((String) item.getContent());
			//InfoEditActivity.jump(this, data, REQUEST_CODE_EDIT_TEXT,TIME);
			DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
					new MispPopWindowListener(){
						@Override
						public void onConfirmClick(String value)
						{
							containbusjson.setStarttime(value);
							ContainBusActivity.this.refreshList(ContainBusActivity.this.getBtnData(false));
						}						
					},this, DateUtil.getCurrentDateTimeStr());
			dateTimePicKDialog.dateTimePicKDialog();
			
		}

		if(ReturnTime.endsWith(title))
		{
			data.setDataValue((String) item.getContent());
			//InfoEditActivity.jump(this, data, REQUEST_CODE_EDIT_TEXT,TIME);
			DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
					new MispPopWindowListener(){
						@Override
						public void onConfirmClick(String value)
						{
							containbusjson.setReturntime(value);
							ContainBusActivity.this.refreshList(ContainBusActivity.this.getBtnData(false));
						}						
					},this, DateUtil.getCurrentDateTimeStr());
			dateTimePicKDialog.dateTimePicKDialog();
			
		}
		if(StartBusPlace.equals(title))
		{
			//HonoRList
			data.setDataValue((String) item.getContent());
			InfoEditActivity.jump(this, data, REQUEST_CODE_EDIT_ADDR,this.getString(R.string.disc_job));
		}
		if(BusStyle.equals(title))
		{
			String[] menuItems1= {
					"福田区",
					"龙华新区",
					"盐田区"
					};
			List<String> list1 = Arrays.asList(menuItems1);	
			MispPopListWindow popWin1 = new MispPopListWindow(this,new MispPopWindowListener(){
				@Override
				public void onConfirmClick(String value)
				{
					containbusjson.setBusstyle(value);
					ContainBusActivity.this.refreshList(ContainBusActivity.this.getBtnData(false));
				}
			},list1);
			popWin1.setLocation(MispPopListWindow.SHOW_CENTER);
			popWin1.setTitle("车型");
			popWin1.setShowTitleFlag(true);
			popWin1.showWindow(view,null);
		}
		
		if(Sum.equals(title))
		{
			//
			data.setDataValue((String) item.getContent());
			InfoEditActivity.jump(this, data, REQUEST_CODE_EDIT_TEXT,this.getString(R.string.disc_note));
		}
		if(Free_Ticket.equals(title))
		{
			//
			data.setDataValue((String) item.getContent());
			InfoEditActivity.jump(this, data, REQUEST_CODE_EDIT_TEXT,this.getString(R.string.disc_note));
		}
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		log.info("onActivityResult: requestCode="+requestCode+",resultCode="+resultCode+",data="+data);
		//重载mispInfo
		super.onActivityResult(requestCode, resultCode, data);
	
		if(REQUEST_CODE_EDIT_ADDR==requestCode ||REQUEST_CODE_EDIT_TEXT == requestCode)
		{
			if(null != data)
			{
				MispEditParameter result_value = (MispEditParameter) data
						.getSerializableExtra(MispTextEditActivity.JUMP_DATA);

				if (null != result_value)
				{
					
					
					if(result_value.getDataKey().equals(StartPlace)){
						containbusjson.setStartplace(result_value.getDataValue());
					}
					
					if(result_value.getDataKey().equals(EndPlace))
					{
						containbusjson.setAimplace(result_value.getDataValue());
					}
					
					if(result_value.getDataKey().equals(StartBusPlace))
					{
						containbusjson.setStartbusplace(result_value.getDataValue());
					}
					if(result_value.getDataKey().equals(BusStyle))
					{
						containbusjson.setBusstyle(result_value.getDataValue());
					}
					if(result_value.getDataKey().equals(Sum))
					{
						Pattern pattern = Pattern.compile("[0-9]*"); 
						   Matcher isNum = pattern.matcher(result_value.getDataValue());
						   if( !isNum.matches() ){
							   showMessage("请输入正确的数字");
						   } 
						   else
						   {
							   containbusjson.setSum(Integer.valueOf(result_value.getDataValue()));
							  
						   }
						
					}
					if(result_value.getDataKey().equals(Free_Ticket))
					{
						Pattern pattern = Pattern.compile("[0-9]*"); 
						   Matcher isNum = pattern.matcher(result_value.getDataValue());
						   if( !isNum.matches() ){
							   showMessage("请输入正确的数字");
						   } 
						   else
						   {
							   containbusjson.setChildrensum(Integer.valueOf(result_value.getDataValue()));
							  
						   }
					
					}
				}
			}
			this.refreshList(this.getBtnData(false));
		}
	}
}
